/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.timesoft.zk.common.event;

import com.timesoft.common.bean.ResponseCommon;
import com.timesoft.common.convertor.Base64ConvertorUtils;
import com.timesoft.global.webservice.WebServiceClient;
import com.timesoft.hibernate.bean.SettingFormatDate;
import com.timesoft.hibernate.bean.SettingFormatNumber;
import com.timesoft.webservice.client.setting.SettingWebService;
import com.timesoft.zk.common.LocaleType;
import com.timesoft.zk.common.ctrl.MainPageWsController;
import com.timesoft.zk.common.message.AlertMessages;
import java.util.Locale;
import org.apache.log4j.Logger;
import org.zkoss.util.Locales;
import org.zkoss.web.Attributes;
import org.zkoss.zk.ui.Executions;

/**
 *
 * @author sorasaks
 */
public class MainEventWsListener {

    private static final Logger LOG = Logger.getLogger(MainEventWsListener.class);

    private final SettingWebService settingService = WebServiceClient.getSettingService();
    private final Base64ConvertorUtils base64 = new Base64ConvertorUtils();

    protected Locale locale;
    protected String contextPath;
    protected String realPath;
    protected String amountFormat;
    protected String dateFormat;

    protected String customerCode;
    protected Boolean authentification;

    public MainEventWsListener() throws Exception {
        initialParameter();
        reloadParameter();
    }

    private void setLocale() {
        Object obj = Executions.getCurrent().getSession().getAttribute(Attributes.PREFERRED_LOCALE);
        Locale localeObj = Locales.getCurrent();
        if (obj == null) {
            Executions.getCurrent().getSession().setAttribute(Attributes.PREFERRED_LOCALE, Locales.getCurrent());
        } else {
            localeObj = (Locale) obj;
        }
        this.locale = (localeObj == null ? LocaleType.US : localeObj);
    }

    private void setRealPath() {
        this.realPath = Executions.getCurrent().getScheme() + "://" + Executions.getCurrent().getServerName()
                + ":" + Executions.getCurrent().getServerPort();
    }

    private void setContextPath() {
        this.contextPath = Executions.getCurrent().getScheme() + "://" + Executions.getCurrent().getServerName()
                + ":" + Executions.getCurrent().getServerPort() + Executions.getCurrent().getContextPath();
    }

    private void setAmountFormat() throws Exception {
        ResponseCommon common = (ResponseCommon) base64.stringToGenericObj(settingService.getSettingFormatNumber());
        if (common.isFlag()) {
            SettingFormatNumber setting = (SettingFormatNumber) base64.stringToGenericObj(common.getValue());
            this.amountFormat = setting.getFormat();
        } else {
            LOG.debug(":::::::::::::: setting ::::::::::::::::" + common);
            AlertMessages.alertMessage(common.getMessage());
        }
    }

    private void setDateFormat() throws Exception {
        ResponseCommon common = (ResponseCommon) base64.stringToGenericObj(settingService.getSettingFormatDate());
        if (common.isFlag()) {
            SettingFormatDate setting = (SettingFormatDate) base64.stringToGenericObj(common.getValue());
            this.dateFormat = setting.getFormat();
        } else {
            LOG.debug(":::::::::::::: setting ::::::::::::::::" + common);
            AlertMessages.alertMessage(common.getMessage());
        }
    }

    private void initialParameter() throws Exception {
        locale = (Locale) Executions.getCurrent().getSession().getAttribute(Attributes.PREFERRED_LOCALE);
        realPath = (String) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_REAL_PATH);
        contextPath = (String) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_CONTEXT_PATH);
        dateFormat = (String) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_DATE_FORMAT);
        amountFormat = (String) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_AMOUNT_FORMAT);
        customerCode = (String) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_CUSTOMER_CODE);
        authentification = (Boolean) Executions.getCurrent().getSession().getAttribute(MainPageWsController.ATTRIBUTE_AUTHENTIFICATION);
    }

    private void reloadParameter() throws Exception {
        if (locale == null) {
            setLocale();
            Executions.getCurrent().getSession().setAttribute(Attributes.PREFERRED_LOCALE, locale);
        }
        if (realPath == null) {
            setRealPath();
            Executions.getCurrent().getSession().setAttribute(MainPageWsController.ATTRIBUTE_REAL_PATH, realPath);
        }
        if (contextPath == null) {
            setContextPath();
            Executions.getCurrent().getSession().setAttribute(MainPageWsController.ATTRIBUTE_CONTEXT_PATH, contextPath);
        }
        if (dateFormat == null) {
            setDateFormat();
            Executions.getCurrent().getSession().setAttribute(MainPageWsController.ATTRIBUTE_DATE_FORMAT, dateFormat);
        }
        if (amountFormat == null) {
            setAmountFormat();
            Executions.getCurrent().getSession().setAttribute(MainPageWsController.ATTRIBUTE_AMOUNT_FORMAT, amountFormat);
        }
    }

}
